<template>
  <div class="login_container">
    <el-row>
      <!-- 平分整个屏幕，当屏幕小于768时,右侧占24份 -->
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_from" :model="loginFrom" :rules="rules" ref="loginForms">
          <h1>hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginFrom.username"
              type="text"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginFrom.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <!-- 保证el-form表单项校验通过再发请求
            -->
            <el-button
              :loading="loading"
              type="primary"
              size="default"
              class="login_btn"
              @click="login"
              >Primary</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
// import {reqLogin} from "../../api/user/index"
import useUserStore from "../../store/modules/user";
import { reactive, ref} from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElNotification } from "element-plus";
//引入获取当前时间的方法
import { gettimer } from "../../utils/time";
// import { rules } from "eslint-plugin-prettier";

//获取el-form组件
let loginForms = ref();

let $router = useRouter();
const userStore = useUserStore();
//收集账号密码
let loginFrom = reactive({ username: "admin", password: "111111" });

//定义加载效果
let loading = ref(false);

//获取路由对象
let $route = useRoute();
const login = async () => {
  //保证表单全部校验通过再发请求
  await loginForms.value.validate();

  //开始加载效果
  loading.value = true;

  try {
    //写法：1：try/catch写法
    //2. .then写法
    await userStore.userLogin(loginFrom)
    //登录成功加载效果消失
    loading.value = false;
    //编程式导航成功跳转到首页
    //判断路由路径是否有query参数，如果有则向query参数条，
    //否则向‘/’条
    let redirect: any = $route.query.redirect;
    $router.push({ path: redirect || "/" });
    location.reload()
    //登录成功提示
    ElNotification({
      type: "success",
      message: "欢迎回来",
      title: `Hi,${gettimer()}好`,
    });
  } catch (error) {
    //登录失败加载效果消失
    loading.value = false;

    //登录失败提示信息

    ElNotification({
      type: "error",
      //断言：error类型（error为ts的内置属性）
      message: (error as Error).message,
    });
  }
  //  .then写法
  // userStore.userLogin(loginFrom).then(result=>{

  // }).catch(error=>{

  // })
};
//自定校验规则
const validatorUserName = (_rule: any, value: any, callback: any) => {
  //rule即为校验规则
  //value即为表单文本对象
  //如果符合条件callback放行通过
  //如果不符合体条件callback方法，注入错误提示消息
  if (value.length >= 5) {
    callback();
  } else {
    callback(new Error("账号长度至少5位"));
  }
};
const validatorPassWord = (_rule: any, value: any, callback: any) => {
  //rule即为校验规则
  //value即为表单文本对象
  //如果符合条件callback放行通过
  //如果不符合体条件callback方法，注入错误提示消息
  if (value.length >= 6) {
    callback();
  } else {
    callback(new Error("密码长度至少6位"));
  }
};

//定义表单校验规则
const rules = {
  username: [
    // {
    //   required: true,
    //   message: "用户名不能为空",
    //   trigger: "blur",
    // },
    // {
    //   min: 6, //文本长度最少6位
    //   max: 10, //文本长度最多10位
    //   message: "账号长度至少6位,最多10位",
    //   trigger: "blur", //触发时机
    // },
    { trigger: "change", validator: validatorUserName },
  ],
  password: [
    // { required: true, message: "密码不能为空", trigger: "blur" },
    // {
    //   min: 6,
    //   max: 10,
    //   message: "密码长度至少6位,最多10位",
    //   trigger: "blur",
    // },
    { trigger: "change", validator: validatorPassWord },
  ],
};
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background-image: url(../../assets/images/background.jpg);
  background-size: cover;
  .login_from {
    padding: 40px;
    display: block;
    position: relative;
    width: 80%;
    top: 30vh;
    background-image: url(../../assets/images/login_form.png);
    background-repeat: no-repeat; /* 设置背景不重复 */
    background-size: cover;
    h1 {
      font-size: 40px;
      color: white;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
